  set more off
  set seed 19112014
  mat drop _all

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*     Providers's Interview Characteristics - Comparison Table
*====================================================================*

* Append data from baseline and follow up
  use          "$prodata1\providers_interview_1st", clear
  append using "$prodata2\providers_interview_2nd"

* Sample (see do file 3_samples)
  sort wave prov_id
  merge 1:1 wave prov_id using "$prodata\sample_by_wave"
  keep if _merge==3
  drop _merge 

* Groups
  gen     group = 1 if treat==0 & wave==1
  replace group = 2 if treat==1 & wave==1
  replace group = 3 if treat==0 & wave==2
  replace group = 4 if treat==1 & wave==2 & WHP==0
  replace group = 5 if treat==1 & wave==2 & WHP==1
  tab group WHP
  list cluster if treat==0 & WHP==1

*====================================================================*
* Table 1: Providers Characteristics at Baseline and Follow Up by Area
* Implementation Areas vs Non-Implementation Areas
*====================================================================*

* Define local with variables to describe
  gen     homeo_ayu = 0
  replace homeo_ayu = 1 if treatment_type_2==1 | treatment_type_3==1

* Consultation Fees in real terms
/*NOTE: Base 2001=100. Price index 2011 = 191.5
                       Price index 2014 = 246.9*/
 gen     c_fee_real = (c_fee/191.5)*100 if wave==1
 replace c_fee_real = (c_fee/246.9)*100 if wave==2

* Table 1
  #delimit ;
  local table1 "age high_school mqualif comp_ever experience npatient work_hs_fac 
                camps fac_gov niindex c_fee_real miss task_1 task_2 task_3 task_4 task_5 
                task_7 miss treatment_type_1 homeo_ayu miss disease_3 disease_4";
  #delimit cr

* Define matrix to store results
  local cword : word count `table1'

  forvalues n = 1/5 {
    mat table1_`n' = J(`cword',2,.)
    local i 1
      foreach var of local table1 {
        if "`var'"=="miss" local i = `i'+1
        else {
          sum `var' if group==`n'
          cap assert inlist(`var',0,1)
          if _rc {
            mat table1_`n'[`i',1] = r(N)
            mat table1_`n'[`i',2] = round(r(mean), .1)
            }
          else {
            mat table1_`n'[`i',1] = r(sum)
            mat table1_`n'[`i',2] = round(r(mean)*100, .1)
          }
      local ++i
      }
    }
    mat table1 = nullmat(table1), table1_`n'
  }

* Column and row names for Table 1
  mat rownames table1 = `table1'
  mat colnames table1 = N_BNI meanB_NI N_BI meanB_I N_FNI meanF_NI N_FI_nowhp meanF_I_nowhp N_FI_whp meanF_I_whp

* Export results
  putexcel set "$results\table1.xlsx", sheet("T1") modify 
  putexcel (J7)  = matrix(table1), hcenter vcenter font(Calibri, 10)


*====================================================================*
* Appendix tables: mean test across different groups
* APPENDIX TABLE A2
* (1) Compare group 1 and group 3
* (2) Compare group 2 ang group 4
* (3) Compare group 4 and group 5
*====================================================================*

  gen     compare13 = .
  replace compare13 = 0 if group==1
  replace compare13 = 1 if group==3
  lab var compare13 "before-after comparison in Non-Implementation Areas"

  gen     compare24 = .
  replace compare24 = 0 if group==2
  replace compare24 = 1 if group==4
  lab var compare24 "before-after comparison in Implementation Areas"

  gen     compare45 = .
  replace compare45 = 0 if group==4
  replace compare45 = 1 if group==5
  lab var compare45 "Non-WHP vs WHP in Implementation Areas"

* Define variables
  #delimit ;
  local table1 "age high_school mqualif comp_ever experience npatient work_hs_fac 
                camps fac_gov niindex c_fee_real miss task_1 task_2 task_3 task_4 task_5 
                task_7 miss treatment_type_1 homeo_ayu miss disease_3 disease_4";
  #delimit cr

* Start loop by comparison group
  foreach c in 13 24 45 {

  * Define matrix to store results
    local cword : word count `table1'
    mat appendix_table1_comp`c'= J(`cword',4,.)
    mat rownames appendix_table1_comp`c' = `table1'
    mat colnames appendix_table1_comp`c' = mean0 mean1 pvalue empty

  * Loop over variables
    local r 1
    foreach var of local table1 {
      if "`var'"=="miss" local i = `i'+1       //jump miss variables//
      else {
        quietly  sum `var'
        cap assert inlist(`var',0,1)         //checking if dummy//

        if _rc {
          quietly  sum `var' if compare`c'==0
          mat appendix_table1_comp`c'[`r',1] = round(r(mean), .1)
          quietly  sum `var' if compare`c'==1
          mat appendix_table1_comp`c'[`r',2] = round(r(mean), .1)
          reg `var' compare`c', cluster(cluster)
          mat appendix_table1_comp`c'[`r',3] = round((2 * ttail(e(df_r), abs(_b[compare`c']/_se[compare`c']))), .001)
          }
        else {
          quietly  sum `var' if compare`c'==0
          mat appendix_table1_comp`c'[`r',1] = round(r(mean)*100, .1)
          quietly  sum `var' if compare`c'==1
          mat appendix_table1_comp`c'[`r',2] = round(r(mean)*100, .1)
          clchi2 `var' compare`c', cluster(cluster)
          mat appendix_table1_comp`c'[`r',3] = round(r(g_pval), .001)
        }
            }
          local r = `r'+1
          }
        }


* Export Appendix Table with mean comparison
  mat appendix_table1_comp=appendix_table1_comp13, appendix_table1_comp24, appendix_table1_comp45
  preserve
  drop _all
  svmat2 appendix_table1_comp, rnames(variables)
  foreach v in 1 2 5 6 9 10 {
    gen     appendix_table1_comp`v'_2 = string(appendix_table1_comp`v', "%8.1f")
    replace appendix_table1_comp`v'_2 = appendix_table1_comp`v'_2+"%" if (_n==2 | _n==3 | _n==4 | _n==8 | _n==9) | (_n>=13 & _n<=18) | (_n>=20 & _n<=21) | (_n>=23 & _n<=24)
    replace appendix_table1_comp`v'_2 ="" if appendix_table1_comp`v'_2 == "."
  }
  order variables, first
  order appendix_table1_comp3 appendix_table1_comp4, after(appendix_table1_comp2_2)
  order appendix_table1_comp7 appendix_table1_comp8, after(appendix_table1_comp6_2)
  order appendix_table1_comp11 appendix_table1_comp12, after(appendix_table1_comp10_2)
  foreach v in 1 2 5 6 9 10 12 {
    drop appendix_table1_comp`v'
  }
  outsheet using "$results\A2_providers.xls", replace
  restore


* Extra test (before-after)
  reg work_hs_fac wave if group!=5, cluster(cluster)
  reg c_fee_real wave if group!=5, cluster(cluster)

